/*
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 *
 * Copyright @2008 the original author or authors.
 */
package com.bluestone.core;

import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.event.KeyEvent;

import com.bluestone.util.Util;

/**
 * CheckAction is the base action for CheckFalseAction and the CheckTrueAction
 * @author <a href="mailto:bluestone.daniel.q@gmail.com">daniel.q</a>
 */
public class CheckAction extends Action {
	protected String spitRegex="\\|";

	public CheckAction(String type, String target, String value, String delay,
			String offset) {
		super(type, target, value, delay, offset);
	}

	static String pageContent = "";

	/**
	 * Init the pageContent.If the value of the target is "newpage",then reset the pageContent. 
	 */
	public boolean execute() {
		if (!super.execute()){
			return false;
		}
		if(proxy){
			return proxyCheck();
		}else{
			return normalCheck();
		}
	}
	
	private boolean normalCheck(){
		try {
			if (target != null && target.indexOf(Attributes.NEWPAGE)>=0) {
				String indexPos = "";
				String x = "";
				String y = "";
				String checkPos = "";
				int pos = target.indexOf("(");
				if(pos>0){
					checkPos = target.substring(pos+1,target.indexOf(")"));
				}else{
					checkPos = Util.getCurrentProject().getBrowser().getCheckPos();
				}								
				x = checkPos.split(",")[0];
				y = checkPos.split(",")[1];
				Page page = Util.getCurrentPage();
				if(page!=null){
					if ("TRUE".equalsIgnoreCase(page.getBrowserBar())) {
						indexPos = x+","
								+ (Util.getCurrentProject().getBarHeight() + Integer.parseInt(y));
					} else {
						indexPos = x+","
								+ (Util.getCurrentProject().getTitlebarHeight() + Integer.parseInt(y));
					}
				}else{
					indexPos = x+","
					+ (Util.getCurrentProject().getBarHeight() + Integer.parseInt(y));					
				}
				
				ActionFactory.createAction(ActionFactory.MOVEACTION,
						Attributes.SCREEN,
						indexPos,
						null, null).execute();
				pageContent = getPageContent();

			}
			if (target != null && target.equalsIgnoreCase(Attributes.CMDWINDOW)) {
				
				String content = "";
				Transferable tText = new StringSelection(content);
				Clipboard sysc = Toolkit.getDefaultToolkit().getSystemClipboard();		
				sysc.setContents(tText, null);
				
				robot.delay(Util.getDelayTime(Util.DELAY1000));
				robot.keyPress(KeyEvent.VK_ALT);
				robot.delay(Util.getDelayTime(Util.DELAY500));
				robot.keyPress(KeyEvent.VK_SPACE);
				robot.delay(Util.getDelayTime(Util.DELAY500));
				robot.keyRelease(KeyEvent.VK_SPACE);
				robot.delay(Util.getDelayTime(Util.DELAY500));
				robot.keyRelease(KeyEvent.VK_ALT);
				robot.delay(Util.getDelayTime(Util.DELAY500));	
				
				robot.keyPress(KeyEvent.VK_E);
				robot.delay(Util.getDelayTime(Util.DELAY300));
				robot.keyRelease(KeyEvent.VK_E);	
				
				robot.keyPress(KeyEvent.VK_S);
				robot.delay(Util.getDelayTime(Util.DELAY300));
				robot.keyRelease(KeyEvent.VK_S);
				
				robot.keyPress(KeyEvent.VK_ENTER);
				robot.delay(Util.getDelayTime(Util.DELAY300));
				robot.keyRelease(KeyEvent.VK_ENTER);				
								
				return getStringFromClipboard();
				
			}
			
			if(target!=null && Attributes.JSWINDOW.equalsIgnoreCase(target)){
				return getStringFromClipboard();
			}
		} catch (Exception me) {
			Util.error(me);
			return false;
		}
		
		return true;
	}
	
	private boolean proxyCheck(){
		return getStringFromClipboard();
	}
	
	private boolean getStringFromClipboard(){
		Clipboard sysc = Toolkit.getDefaultToolkit().getSystemClipboard();		
		Transferable clipT = sysc.getContents(null);
		if (clipT.isDataFlavorSupported(DataFlavor.stringFlavor)) {
			try {
				pageContent = (String) clipT
						.getTransferData(DataFlavor.stringFlavor);
			} catch (Exception e) {
				e.printStackTrace();
				return false;
			}
		}
		return true;
	}

	protected String getPageContent() {
		String content = "";
		Transferable tText = new StringSelection(content);
		Clipboard sysc = Toolkit.getDefaultToolkit().getSystemClipboard();		
		sysc.setContents(tText, null);
		
		robot.mousePress(KeyEvent.BUTTON3_MASK);
		robot.delay(Util.getDelayTime(Util.DELAY300));
		robot.mouseRelease(KeyEvent.BUTTON3_MASK);

		robot.delay(Util.getDelayTime(Util.DELAY1000));
		robot.keyPress(KeyEvent.VK_V);
		robot.delay(Util.getDelayTime(Util.DELAY500));
		robot.keyRelease(KeyEvent.VK_V);
		robot.delay(Util.getDelayTime(Util.DELAY1000));

		robot.keyPress(KeyEvent.VK_CONTROL);
		robot.delay(Util.getDelayTime(Util.DELAY300));
		robot.keyPress(KeyEvent.VK_A);
		robot.delay(Util.getDelayTime(Util.DELAY300));
		robot.keyRelease(KeyEvent.VK_A);
		robot.delay(Util.getDelayTime(Util.DELAY300));
		robot.keyRelease(KeyEvent.VK_CONTROL);

		robot.delay(Util.getDelayTime(Util.DELAY500));

		robot.keyPress(KeyEvent.VK_CONTROL);
		robot.delay(Util.getDelayTime(Util.DELAY300));
		robot.keyPress(KeyEvent.VK_C);
		robot.delay(Util.getDelayTime(Util.DELAY300));
		robot.keyRelease(KeyEvent.VK_C);
		robot.delay(Util.getDelayTime(Util.DELAY300));
		robot.keyRelease(KeyEvent.VK_CONTROL);

		robot.delay(Util.getDelayTime(Util.DELAY500));

		Transferable clipT = sysc.getContents(null);
		if (clipT.isDataFlavorSupported(DataFlavor.stringFlavor)) {
			try {
				content = (String) clipT
						.getTransferData(DataFlavor.stringFlavor);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		if("".equals(content)){
			return content;
		}
		robot.keyPress(KeyEvent.VK_ALT);
		robot.delay(Util.getDelayTime(Util.DELAY300));
		robot.keyPress(KeyEvent.VK_F4);
		robot.delay(Util.getDelayTime(Util.DELAY300));
		robot.keyRelease(KeyEvent.VK_F4);
		robot.delay(Util.getDelayTime(Util.DELAY300));
		robot.keyRelease(KeyEvent.VK_ALT);

		
		return content;
	}
	
	public static void main(String[] args){
		String value="Event|Detail|Report:|All|Locations";
		String[] values = value.split("\\|");
		System.out.println(values[0]);
	}

}
